<?php

namespace app\admin\controller;

use app\admin\model\ExaminationCategory;
use app\admin\model\ExaminationPaper;
use app\admin\model\ExaminationQuestion;
use app\admin\service\ExaminationService;
use think\facade\Request;

class Examination extends Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    public function category()
    {
        if (Request::isAjax()) {
            return $this->getJson(ExaminationCategory::getList());
        }
        return $this->fetch();
    }

    public function editCategory($id)
    {
        if (Request::isAjax()) {
            return $this->getJson(ExaminationService::goEditCategory());
        }
        return $this->fetch('', ['data' => ExaminationCategory::find($id)]);
    }

    public function goCategoryStatus()
    {
        return $this->getJson(ExaminationService::goCategoryStatus());
    }

    public function goCategoryRemove()
    {
        return $this->getJson(ExaminationService::goCategoryRemove());
    }

    public function index()
    {
        if (Request::isAjax()) {
            return $this->getJson(ExaminationPaper::getList());
        }
        return $this->fetch();
    }

    public function edit($id)
    {
        if (Request::isAjax()) {
            return $this->getJson(ExaminationService::goEdit());
        }
        $this->assign('categoryList', ExaminationCategory::getAll());
        return $this->fetch('', ['data' => ExaminationCategory::find($id)]);
    }

    public function goRemove()
    {
        return $this->getJson(ExaminationService::goRemove());
    }

    public function questions($pid)
    {
        if (Request::isAjax()) {
            return $this->getJson(ExaminationQuestion::getList($pid));
        }
        $this->assign('pid', $pid);
        return $this->fetch();
    }

    public function editQuestion($pid, $id)
    {
        if (Request::isAjax()) {
            return $this->getJson(ExaminationService::goEditQuestion());
        }
        $questionList = [];
        $data = ExaminationQuestion::getInfoById($id);
        if ($data) {
            $questionList[] = [
                'id' => $data['qid'],
                'title' => $data['title'],
            ];
        }
        $this->assign('questionList', $questionList);
        $this->assign('pid', $pid);
        return $this->fetch('', ['data' => $data]);
    }

    public function goQuestionRemove()
    {
        return $this->getJson(ExaminationService::goRemoveQuestion());
    }
}